//
//  GGRaceDetailVC.swift
//  DongBaoDemo
//
//  Created by 陈志勇 on 2018/8/5.
//  Copyright © 2018年 陈志勇. All rights reserved.
//

import UIKit

class GGRaceDetailVC: UIViewController {
    
    @IBOutlet weak var titleViewBg: UIView!
    @IBOutlet weak var team1ImageView: UIImageView!
    @IBOutlet weak var tem1NameLabel: UILabel!
    @IBOutlet weak var team2ImageView: UIImageView!
    @IBOutlet weak var team2Label: UILabel!
    @IBOutlet weak var startTimeLabel: UILabel!
    @IBOutlet weak var vsLabel: UILabel!
    
    @IBOutlet weak var tableView: UITableView!
    
    var messageVMs: [MessageViewModel] = []
    var schedule: Schedule?

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        navigationController?.setNeedsNavigationBackground(alpha: 0)
        view.backgroundColor = .black

        setupUI()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib.init(nibName: "GGMessageCell", bundle: nil), forCellReuseIdentifier: "GGMessageCell")
        tableView.tableFooterView = UIView()
        
        requestRaceDetail()

    }


    init(schedule: Schedule) {
        super.init(nibName: nil, bundle: nil)
        self.schedule = schedule
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension GGRaceDetailVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if messageVMs.count == 0 {
            return 1
        }
        
        return messageVMs.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if messageVMs.count == 0 {
            return GGMessageCell()
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "GGMessageCell", for: indexPath) as! GGMessageCell
        cell.messageVM = messageVMs[indexPath.row]
        
        return cell
    }
}

extension GGRaceDetailVC {
    private func setupUI() {
        
        // 头部信息设置
        guard let schedule = schedule else {
            fatalError()
        }
        
        if let status = schedule.status {
            switch status {
            case .enroll:
                startTimeLabel.text = "未开始"
            case .race:
                // 请求
                requestRaceDetail()
            case .over:
                startTimeLabel.text = "已结束"
            }
        }
        let team1url = "http://football.dooksport.com" + schedule.team1_image!
        team1ImageView.kf.setImage(with: URL(string: team1url))
        let team2url = "http://football.dooksport.com" + schedule.team2_image!
        team2ImageView.kf.setImage(with: URL(string: team2url))
        
        tem1NameLabel.text = schedule.team1_shortname
        team2Label.text = schedule.team2_shortname
        vsLabel.text = "\(schedule.team1_score!)   -   \(schedule.team2_score!)"
//        if let startTime = schedule.start_time, let time = Double(startTime) {
//            let date = Date(timeIntervalSince1970: time)
//            let dateStr = date.toString(dateFormat: "yyyy-MM-dd HH:mm")
//            startTimeLabel.text = dateStr
//        }
        
        // title设置
        let titleView = GGTitleView(frame: CGRect(x: 0, y: 0, width: GGScreenW, height: 44), titles: ["赛况", "阵容", "数据", "讨论"], titleColor: .black)
        titleViewBg.addSubview(titleView)
        titleView.didSelectTitle { (index) in
            print("didSelectTitle \(index)")
        }
    }
}

extension GGRaceDetailVC {
    func requestRaceDetail() {
        guard let id = schedule?.id else {
            fatalError()
        }
        NetWorkTool.shared.loadRaceDetail(raceID: id) { (response) in
            guard let messageResponse = ResponseRaceDetail.deserialize(from: response.data?.info) else {
                return
            }
            
            guard let messages = messageResponse.message else {
                return
            }
            self.messageVMs = messages.map({ MessageViewModel(message: $0) })
            self.tableView.reloadData()
        }
    }
}
